{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a502bb8f-b748-4547-9f28-b4be4c663b84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== 课程成绩统计系统 =====\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入学生姓名 (输入'结束'完成录入):  姚瑜巧\n",
      "请输入姚瑜巧的成绩:  95\n",
      "请输入学生姓名 (输入'结束'完成录入):  xxx\n",
      "请输入xxx的成绩:  100\n",
      "请输入学生姓名 (输入'结束'完成录入):  yyy\n",
      "请输入yyy的成绩:  zzz\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入无效，请输入有效的数字成绩。\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入学生姓名 (输入'结束'完成录入):  zzz\n",
      "请输入zzz的成绩:  99\n",
      "请输入学生姓名 (输入'结束'完成录入):  结束\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "===== 成绩统计结果 =====\n",
      "平均分: 98.00\n",
      "最高分: 100.0\n",
      "最低分: 95.0\n",
      "\n",
      "===== 学生成绩排名 =====\n",
      "1. xxx: 100.0\n",
      "2. zzz: 99.0\n",
      "3. 姚瑜巧: 95.0\n"
     ]
    }
   ],
   "source": [
    "# 学生成绩统计系统\n",
    "\n",
    "def input_grades():\n",
    "    \"\"\"输入学生成绩并返回成绩字典\"\"\"\n",
    "    grades = {}\n",
    "    while True:\n",
    "        student_name = input(\"请输入学生姓名 (输入'结束'完成录入): \")\n",
    "        if student_name == \"结束\":\n",
    "            break\n",
    "        try:\n",
    "            score = float(input(f\"请输入{student_name}的成绩: \"))\n",
    "            grades[student_name] = score\n",
    "        except ValueError:\n",
    "            print(\"输入无效，请输入有效的数字成绩。\")\n",
    "    return grades\n",
    "\n",
    "def calculate_statistics(grades):\n",
    "    \"\"\"计算成绩统计信息\"\"\"\n",
    "    if not grades:\n",
    "        return {\"平均分\": 0, \"最高分\": 0, \"最低分\": 0}\n",
    "    \n",
    "    scores = list(grades.values())\n",
    "    return {\n",
    "        \"平均分\": sum(scores) / len(scores),\n",
    "        \"最高分\": max(scores),\n",
    "        \"最低分\": min(scores)\n",
    "    }\n",
    "\n",
    "def sort_students_by_grade(grades, reverse=True):\n",
    "    \"\"\"按成绩排序学生\"\"\"\n",
    "    return sorted(grades.items(), key=lambda x: x[1], reverse=reverse)\n",
    "\n",
    "def main():\n",
    "    print(\"===== 课程成绩统计系统 =====\")\n",
    "    grades = input_grades()\n",
    "    \n",
    "    if not grades:\n",
    "        print(\"未输入任何成绩，程序结束。\")\n",
    "        return\n",
    "    \n",
    "    # 计算统计信息\n",
    "    stats = calculate_statistics(grades)\n",
    "    \n",
    "    # 显示统计结果\n",
    "    print(\"\\n===== 成绩统计结果 =====\")\n",
    "    print(f\"平均分: {stats['平均分']:.2f}\")\n",
    "    print(f\"最高分: {stats['最高分']}\")\n",
    "    print(f\"最低分: {stats['最低分']}\")\n",
    "    \n",
    "    # 显示排名\n",
    "    print(\"\\n===== 学生成绩排名 =====\")\n",
    "    sorted_students = sort_students_by_grade(grades)\n",
    "    for rank, (name, score) in enumerate(sorted_students, 1):\n",
    "        print(f\"{rank}. {name}: {score}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59faddd9-ef63-497d-b2d0-f05f61b4f8ae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
